package com.woniuxy.microgoodsserver.brandmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.BrandInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo2;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microgoodsserver.brandmag.mapper.BrandMapper;
import com.woniuxy.microgoodsserver.brandmag.service.BrandService;
import com.woniuxy.microgoodsserver.sortmag.service.GoodCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**品牌实现类
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/25 15:04
 */
@Service
@Transactional(readOnly = true)
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandMapper brandMapper;
    @Resource
    private GoodCategoryService goodCategoryServiceImpl;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void addBrand(BrandInfo brandInfo) {

        //根据选择的子目类型查询子目的ID
        GoodCategoryInfo2 goodCategoryInfo2 = goodCategoryServiceImpl.findSonIdByName(brandInfo.getBrandType());

        //添加品牌
        brandMapper.addBrand(brandInfo);

        //查询新添加品牌的ID
        BrandInfo brandInfo1 = findByName(brandInfo);

        //为子目---品牌表添加数据
        brandMapper.addSortAndBran(goodCategoryInfo2.getId(),brandInfo1.getId());
    }
    /**
        根据品牌名称查询
     * @params:
     * @return
     */
    @Override
    public BrandInfo findByName(BrandInfo brandInfo) {
         //根据品牌名称和品牌类型判断该对应品牌是否存在
        return  brandMapper.findByName(brandInfo);
    }

    /**分页查询所有的品牌

     * @params:
     * @return
     */
    @Override
    public PageInfo<BrandInfo> findBrand(PageRequest pageRequest) {
        return PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getLimit()).doSelectPageInfo(()->{
            brandMapper.findBrand();
        });
    }

    /**
        删除品牌
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteBrand(BrandInfo brandInfo) {
        brandMapper.deleteBrand(brandInfo);
    }
    /**
     根据ID查询品牌信息
     * @params:
     * @return
     */
    @Override
    public BrandInfo findById(BrandInfo brandInfo) {
        return brandMapper.findById(brandInfo);
    }
    /**
        修改品牌信息
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void updateBrand(BrandInfo brandInfo,String name) {

        //根据原来的子目名字查询对应的ID值
        int sonId = goodCategoryServiceImpl.findSonIdByName(name).getId();
        /*
        * 如果要修改这个品牌对应的所属子目
        * 那么需要去查询子目的ID
        * 根据子目名查询ID
        */
        GoodCategoryInfo2 goodCategoryInfo2 = goodCategoryServiceImpl.findSonIdByName(brandInfo.getBrandType());
        //修改中间表,需要查询被修改之前的中间表这个品牌对应的ID

        brandMapper.updateBrandAndSort(brandInfo,goodCategoryInfo2,sonId);
        brandMapper.updateBrand(brandInfo);
    }

    /**
     根据子目ID查询下面所有品牌
     * @params:
     * @return
     */
    @Override
    public List<BrandInfo> findBrandBySort(GoodCategoryInfo2 goodCategoryInfo2) {
        return brandMapper.findBrandBySort(goodCategoryInfo2);
    }
}
